/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Animation.Animations;

import Animation.Animation;
import Scenery.BaseCharacter;
import Scenery.BaseSprite;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import main.DumbMath;

/**
 *
 * @author usuario
 * 
 */

public class PickUp extends Animation {

    private float bodyInc = 0f;
    private float bodySpeed = 0.02f;
    private float handX = 0f;
    private Vector3f handPosi;
    private Geometry picked = null;
    
    @Override
    public void setParameter(Object p) {
        if(p instanceof Geometry) {
            picked = (Geometry)p;
        }
    }
    
    public PickUp(BaseCharacter s, int frames){
        super(s, frames);
        storedValues = 2;
        frozenOnPerform = true;
        buildAnimation();
    }
    
    @Override
    protected void buildFrame() {
        if(frame < maxFrame/2) {
            bodyInc = -bodySpeed;
            handX = bodySpeed;
        } else {
             bodyInc = +bodySpeed;
             handX = bodySpeed*1.1f;
        }
        animationFrames[frame][0] = bodyInc;
        animationFrames[frame][1] = handX;
    }

    @Override
    public void performFrame() {
       handPosi = animationBody.getLhand().getLocalTranslation();
       if(picked!=null)
        picked.getControl(RigidBodyControl.class).setPhysicsLocation(handPosi);
       animationBody.getLhand().getLocalTranslation().addLocal(new Vector3f(-animationFrames[frame][0],animationFrames[frame][1],0).mult(1.6f));
       animationBody.getRhand().getLocalTranslation().addLocal(new Vector3f(-animationFrames[frame][0],animationFrames[frame][1] ,0).mult(1.6f));
       animationBody.getHeadNode().getLocalTranslation().addLocal(new Vector3f(0,animationFrames[frame][0],0).mult(1.6f));
       animationBody.getBodyNode().getLocalTranslation().addLocal(new Vector3f(0,animationFrames[frame][0],0));
       if(frame == maxFrame-1) { 
          frame = 0;
           this.canceled = true;
           this.finished = true;
       }
    }

    private Vector3f bodyPos;
    private Vector3f headPos;
    private Vector3f rhandPos;
    private Vector3f lhandPos;
    
    @Override
    public void gotoInitialState() {
       bodyInc = 0f;
       picked = null;
       animationBody.getBodyNode().setLocalTranslation(bodyPos);
       animationBody.getHeadNode().setLocalTranslation(headPos);
       animationBody.getRhand().setLocalTranslation(rhandPos);
       animationBody.getLhand().setLocalTranslation(lhandPos);
       frame = 0;
       canceled = false;
       finished = false;
    }

    @Override
    public String getName() {
        return "PickUp";
    }

    @Override
    public void saveInitialState() {
        lhandPos = animationBody.getLhand().getLocalTranslation().clone();
        rhandPos = animationBody.getRhand().getLocalTranslation().clone();
        bodyPos =  animationBody.getBodyNode().getLocalTranslation().clone();
        headPos =  animationBody.getHeadNode().getLocalTranslation().clone();
    }

    @Override
    protected float getCicleRunTime() { return 0f;}
    
}
